package com.example.demo.controller;

import com.example.config.RedisUtil;
import com.example.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenlin
 */
@Controller
@RequestMapping("/usersDemo")
public class UserController {
    private static Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    UserService userService;
    /**
     * 使用redis
     *
     * 这里演示redis的 set get
     * 其他方法请直接打开RedisUtil.java查看
     **/
    @Resource
    RedisUtil redisService;

    @ResponseBody
    @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public List<Map<String, Object>> test() {
        log.info("进入了test方法！");
        List<Map<String, Object>> list = userService.userQueryAll();
        //redis设置key，设置300秒有效期
        redisService.set("list",list,300);
        //取出来打印,
        System.out.println(redisService.get("list"));
        return list;
    }

    @RequestMapping(value = "/testHtml", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public String testHtml(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> list = userService.userQueryAll();
        request.setAttribute("list", list);
        log.info("进入了testHtml方法！");
        return "views/testHtml";
//        ModelAndView mav =new ModelAndView("views/testHtml");
//        mav.addObject("list", list);
//        log.info("进入了testHtml方法！");
//        return mav;
    }
    @RequestMapping(value = "/testHtml2", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public void testHtml2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map<String, Object>> list = userService.userQueryAll();
        request.setAttribute("list", list);
        log.info("进入了testHtml2方法！");
        request.getRequestDispatcher("/WEB-INF/views/testHtml.html").forward(request, response);
    }

    @RequestMapping(value = "/testJsp", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public void testJsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map<String, Object>> list = userService.userQueryAll();
        request.setAttribute("list", list);
        log.info("进入了testJsp方法！");
        request.getRequestDispatcher("/WEB-INF/views/testJsp.jsp").forward(request, response);
    }
    @RequestMapping(value = "/testJsp2", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public void testJsp2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map<String, Object>> list = userService.userQueryAll();
        ModelAndView mav =new ModelAndView("/WEB-INF/views/testJsp.jsp");
        mav.addObject("list", list);
        request.setAttribute("list", list);
        request.getRequestDispatcher("/WEB-INF/views/testJsp.jsp").forward(request, response);
    }

    @RequestMapping(value = "/reactDemo", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public String reactDemo(HttpServletRequest request, HttpServletResponse response) {

        return "views/reactDemo";
    }


    //            request.getRequestDispatcher("/WEB-INF/views/testHtml.html").forward(request, response);
    @ResponseBody
    @RequestMapping(value = "/usersAll", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public Map<String, Object> usersAll(Model m,
                                        @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        log.info(String.valueOf(map));
        return map;
    }
}
